﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SMG.Domain.Repository.Migration;
using SMG.Application.DTO.Migration;
using SMG.Application.Extension;
using SMG.ServiceContract.Migration;
using Infrastructure.Core.Common;
using SMG.Domain.Model.Migration;
using SMG.Domain.Repository;
using SMG.Domain.Specifications;
using SMG.Domain.Specifications.Migration;

namespace SMG.Application.Migration
{
    public class PlayServiceImpl : ApplicationService, IPlayService
    {
        private IPlayRecordRepository playRecordRepository;

        public PlayServiceImpl(IRepositoryContext context,
                                    IPlayRecordRepository playRecordRepository)
            : base(context)
        {
            this.playRecordRepository = playRecordRepository;
            this.playRecordRepository.SetContext(context);
        }

        /// <summary>
        /// 获取图文表单列表
        /// </summary>
        /// <param name="channelId"></param>
        /// <param name="playType"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageResult<PlayRecordDto> QueryPlayByPage(string channelId, string playType, int pageIndex, int pageSize)
        {
            var result = new PageResult<PlayRecordDto>()
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                TotalRecords = 0,
            };
            var type = Convert.ToInt32(playType);
            var specification = new AndSpecification<PlayRecord>(new PlayChannelIdEqualSpecification(channelId),
                                                                    new PlayTypeEqualSpecification(type));
            var plays = playRecordRepository.QueryByPage(specification, p => p.PlayDate, SortOrder.Descending, pageSize, pageIndex);
            if (plays != null)
            {
                result.TotalRecords = plays.TotalRecords;
                result.Data = plays.Data.ConvertToPlayRecordDto();
            }
            return result;
        }
    }
}
